<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //定义一个带有递归的函数，实现对象的深度拷贝

        // 拷贝函数
        function myClone(data){
            // 判断当前参数data是否是对象，若是则进行克隆，否则直接返回
            if(data instanceof Object && data !== null){
                // 根据data的数据类型进行创建容器（对象或数组）
                let res = Array.isArray(data)? [] : {};
                // 遍历并进行子属性的复制
                for(let k in data){
                    //res[k] = data[k];
                    res[k] = myClone(data[k]);
                }
                return  res;
            } else {
                return data;
            }
        }

        // 测试
        const a = [1, [2,3], 4,{aa:"bb",cc:"dd"}];
        const b = myClone(a);
        a[0] = 200; 
        a[1][0] = 100;
        a[3].aa = "ee";
        console.log(a);
        console.log(b);



        //const a = [10,20,30];
        //const a = {name:'lisi',age:20};
        // const a = null;
        // const a2 = myClone(a);
        // //a[1] = "aaa";
        // //a["age"] = 30;
        // console.log(a); 
        // console.log(a2); 
    </script>
</body>
</html>